package work.igames.vertxcreate.utils;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.springframework.stereotype.Component;
import work.igames.vertxcreate.common.CommonResult;
import work.igames.vertxcreate.constant.StateEnum;
import work.igames.vertxcreate.enitity.UserSkill;

/**
 * @ClassName HttpUtil
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2020/11/2 上午11:50
 * @Version V1.0
 * @Since JDK 11
 */
public class HttpUtil {

    public static void writeJsonResponse(RoutingContext routingContext, Object o) {
        HttpServerResponse response = routingContext.response();
        response.putHeader(HttpHeaderNames.CONTENT_TYPE, "application/json")
                .setChunked(true)
                .write(JsonObject.mapFrom(o).toString()).end();
    }

    public static void writeCommonJsonResponse(RoutingContext routingContext, Object o, StateEnum stateEnum) {
        CommonResult<Object> objectCommonResult = CommonResult.create(o, stateEnum);
        HttpServerResponse response = routingContext.response();
        response.putHeader(HttpHeaderNames.CONTENT_TYPE, "application/json")
                .setChunked(true)
                .write(JsonObject.mapFrom(objectCommonResult).toString()).end();
    }
}
